﻿using BLL;
using Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Script.Serialization;
using Newtonsoft.Json;
namespace WebApplication01.AssetHandel
{
    /// <summary>
    /// DeHandler 的摘要说明
    /// </summary>
    public class DeHandler : IHttpHandler
    {
        DeptInfoBLL bll = new DeptInfoBLL();
        public void ProcessRequest(HttpContext context)
        {
            //context.Response.ContentType = "text/plain";
            //context.Response.Write("Hello World");
            string stype = context.Request["sType"];
            string result = string.Empty;
            int DepID = Convert.ToInt32(context.Request["ID"]);
            if (stype == "list")
            {
                string dName = context.Request["deptName"];
                int offset = Convert.ToInt32(context.Request["offset"]);
                int limit = Convert.ToInt32(context.Request["limit"]);
                int begin = offset + 1;
                int end = offset + limit;
                int total;
                List<DeptInfo> list = bll.getDept(dName, begin, end, out total);
                JavaScriptSerializer s = new JavaScriptSerializer();
                var json = new
                {
                    total = total,
                    rows = list
                };
                result = s.Serialize(json);
                context.Response.Write(result);
            }
            else if (stype == "select")
            {
                //下拉列表,list没有参数
                string dapt = context.Request["dapt"];
                List<DeptInfo> list = bll.Select(dapt);
                result = JsonConvert.SerializeObject(list);
                context.Response.Write(result);
            }
            else if (stype == "update")
            {
                DeptInfo det = new DeptInfo();
                det.DeptName = context.Request["rName"];
                det.DeptReMark = context.Request["ReMark"];
                if (DepID == 0)
                {
                    result = bll.Add(det);
                }
                else
                {
                    det.DeptID = DepID;
                    result = bll.Update(det);
                }
                context.Response.Write(result);
            }
            else if (stype=="Delete") {
                int cid = Convert.ToInt32(context.Request["id"]);
                result = bll.Delete(cid);
                context.Response.Write(result);
            }
        }
        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}